#include "server.h"
#define STR_LEN 16

char* generateStr(){
    char str[STR_LEN + 1] = {0};
    int i, flag;

    srand(time(NULL));
    for(i = 0; i < STR_LEN; i++){
        flag = rand() % 3;
        switch(flag){
            case 0:
                str[i] = rand() % 26 +'a';
                break;
            case 1:
                str[i] = rand() % 26 + 'A';
                break;
            case 2:
                str[i] = rand() % 10 + '0';
                break;
        }
    }
    char* salt = (char*)malloc(sizeof(str) + 4);
    bzero(salt, sizeof(salt));
    strcpy(salt, "$6$"); //$6$ 使用SHA512
    strcat(salt, str);
    strcat(salt, "$");  //str(len) = 20
    //printf("%ld\n", sizeof(salt));
    return salt;
}

char* loginVerify(int netfd, MYSQL* mysql){
    char userInfo[1024] = {0};
    train_t train;
    int length;
    int ret = recv(netfd, &length, sizeof(length), 0);
    ret = recv(netfd, userInfo, length, 0);
    //printf("ret = %d\n", ret);
    //printf("userInfor = %s\n", userInfo);
    //分离用户名和密码
    //检验用户名
    char *username = strtok(userInfo, "/");
    char sql1[1024] = "select username from user where username = '";
    strcat(sql1, username);
    strcat(sql1, "';");
    int qret = mysql_query(mysql, sql1);
    MYSQL_RES* res1 = mysql_store_result(mysql);
    if(mysql_num_rows(res1) == 0){
        ret = 0;
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        printf("%d netfd input username error\n",netfd);
        mysql_free_result(res1);
        bzero(username, sizeof(username));
        strcpy(username, "failed");
        return username;
    }

    char *password = strtok(NULL, "/");
    //从sql中获取用户对应的盐值和密文密码
    char sql2[1024] = "select A.salt, A.password from user as A where username = '";
    strcat(sql2, username);
    strcat(sql2, "';");
    //printf("sql = %s\n", sql);
    qret = mysql_query(mysql, sql2);

    MYSQL_RES* res2 = mysql_store_result(mysql);
    MYSQL_ROW row2 = mysql_fetch_row(res2);

    char *tableSalt = row2[0];
    //printf("tableSalt = %s\n", tableSalt);
    char *tableCry = row2[1];
    //printf("tableCry = %s\n", tableCry);
    mysql_free_result(res2);
    //生成完整加密密码
    char tablePassword[1024] = {0};
    strcat(tablePassword, tableCry);
    //printf("tablePassword = %s\n", tablePassword);
    char *cryPassword = crypt(password, tableSalt);
    //printf("cryPassword = %s\n tablePassword = %s \n", cryPassword, tablePassword);
    
    if(strcmp(cryPassword, tablePassword) == 0){ //验证密码成功
        printf("%d netfd success into.\n",netfd);
        ret = 1;
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        return username;
    }else{  //验证密码失败
        printf("%d netfd input passwords error\n",netfd);
        ret = 0;
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        bzero(username, sizeof(username));
        strcpy(username, "failed");
        return username;
    }

    /*test
    //printf("%d\n", mysql_num_rows(res));
    //printf("123 = %s\n", tableSalt);
    printf("tablePassword = %s\n", tablePassword);
    //char* userSalt = 
    printf("username = %s\n", username);
    printf("password = %s\n", password);
    //char* salt = generateStr();
    
    char salt[] = "$6$tm3Md1t21zreQIxt$";

    printf("%s\n", salt);
    printf("%ld\n", strlen(salt));
    char* encrPwd = crypt(password, salt);
    printf("%s\n", encrPwd);
    printf("%ld\n", strlen(encrPwd));
    
    char* encrPwd = crypt(password, salt);
    */
   
}
